package org.gwt.client.vista.figuras;

import org.vaadin.gwtgraphics.client.shape.Path;

import com.google.gwt.user.client.Window;

public class Plano {
	private Path p;
	private double opacidad;
	private int cx,cy,cz,c;
	private int x,y,z,x1,y1;
	private int rx,ry;
	private int escala;
	
	public Plano(){
	rango(1	, 1);	
	}
	
	public void setOpacidad(double opac) {
		this.opacidad=opac;
	}
	public void setOrigen(int x1,int y1){
		this.x1=x1;
		this.y1=y1;		
	}
	
	public void rango(int rx,int ry){
		this.rx=x;
		this.ry=y;
	}
	public void conver3D_2D(int x,int y,int z){		
		int x_1=escala*x;
		int y_1=escala*y;
		int z_1=escala*z;
		int d=(int) Math.sqrt(Math.pow(z_1, 2)/2);
		if(x<=0&&z<=0){
			this.x=x1+x_1+d;
			this.y=y1-y_1-d;
		}
		else if(z<0){
			this.x=x1+d+x_1;
			this.y=y1-d-y_1;
		}
		else{
			this.x=x1+x_1-d;
			this.y=y1-y_1+d;
		}
		Window.alert(
				x+","+y+","+z
				+"\n"+this.x+","+this.y
				);
	}
	
	public void setEscala(int esc){
		this.escala=esc;
	}
		
	public void setEcuacion(int x,int y,int z,int c){
		this.cx=x;
		this.cy=y;
		this.cz=z;
		this.c=c;
	}
	
	public void resolverEcuacion(int x,int y){
		if(cz==0) return;
		z=(cx*x+cy*y+c)/cz;
		if(cz>0) z=-z;
		conver3D_2D(x, y, z);
	}
	
	public void mover(int x,int y,int z){
		conver3D_2D(x, y, z);
		p.setX(x);
		p.setY(y);
	}
	
	public Path dibujar(String color){
		int x_1,y_1;
		if(cz==0) return null;
		resolverEcuacion(0,0);
		x_1=x;
		y_1=y;
		p=new Path(x, y);
		resolverEcuacion(0, 5);
		x_1=x-x_1;
		y_1=y-y_1;
		p.lineRelativelyTo(x_1, y_1);
		x_1=x;
		y_1=y;
		resolverEcuacion(5,5);
		x_1=x-x_1;
		y_1=y-y_1;
		p.lineRelativelyTo(x_1, y_1);
		x_1=x;
		y_1=y;
		resolverEcuacion(5, 0);
		x_1=x-x_1;
		y_1=y-y_1;
		p.lineRelativelyTo(x_1, y_1);
		p.close();
		p.setFillColor(color);
		p.setFillOpacity(opacidad);		
		return p;
	}
}

